<?php
require_once('merchant_output_fns.php');
//检查是否已开店
function check_have_merchant()
{
    require_once('db_fns.php');
    $conn = db_connect();
    $result = $conn->query("select `mid` from `user` where uid='" . $_SESSION['uid'] . "'");
    $mid = $result->fetch_array()['mid'];
    return $mid != null;
}
//申请开店
function apply_merchant($user_id, $name, $intro, $address, $tel)
{
    require_once('db_fns.php');
    $db = db_connect();
    $query = "call sp_create_merchant($user_id, '$name', '$intro', '$address', '$tel')";
    return $db->query($query);
}
//检查用户是否为商家
function check_merchant(){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $uid = $_SESSION['uid'];
    $sql = "select mid from user where uid = {$uid}";
    $result = $conn->query($sql)->fetch_array()['mid'];
    return $result;
}
//获取商家店名
function get_merchant_name(){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $uid = $_SESSION['uid'];
    $sql = "select mid from user where uid = {$uid}";
    $mid = $conn->query($sql)->fetch_array()['mid'];
    $sql = "select name from merchant where mid = {$mid}";
    $result = $conn->query($sql)->fetch_array()['name'];
    return $result;
}
//获取菜品简介
function get_dishes_info($did){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $sql = "select info from merchant_dishes where did = {$did}";
    $result = $conn->query($sql)->fetch_array()['info'];
    return $result;
}
//新增菜品
function release_dishes($name, $info, $price){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $uid = $_SESSION['uid'];
    $sql = "select mid from user where uid = {$uid}";
    $mid = $conn->query($sql)->fetch_array()['mid'];
    $sql = "insert into merchant_dishes (mid, name, info, price) values ({$mid}, '{$name}', '{$info}', {$price})";
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('发布成功！');location.href='merchant_center.php';</script>";
    } else {
        echo "<script>alert('发布失败！');location.href='merchant_center.php';</script>";
    }
}
//修改菜品信息
function update_dishes($did,$info,$price){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $sql = "update merchant_dishes set info = '{$info}', price = {$price} where did = {$did}";
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('修改成功！');location.href='merchant_center.php';</script>";
    } else {
        echo "<script>alert('修改失败！');location.href='merchant_center.php';</script>";
    }
}
//修改菜品状态(上架/下架)
function change_dishes_state($did,$state){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    if($state==1){
        $sql = "update merchant_dishes set state = '在售' where did = {$did}";
    }
    if($state==0){
        $sql = "update merchant_dishes set state = '售罄' where did = {$did}";
    }
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('修改成功！');location.href='merchant_center.php';</script>";
    } else {
        echo "<script>alert('修改失败！');location.href='merchant_center.php';</script>";
    }
}
//获取菜品信息
function get_dishes(){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $uid = $_SESSION['uid'];
    $sql = "select mid from user where uid = {$uid}";
    $mid = $conn->query($sql)->fetch_array()['mid'];
    $sql = "select * from merchant_dishes where mid = {$mid}";
    $result = $conn->query($sql);
    return $result;
}
//修改菜品推荐位
function change_dish_recommend($did,$reced){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    if($reced==1){
        $sql = "update merchant_dishes set reced = 'Y' where did = {$did}";
    }
    if($reced==0){
        $sql = "update merchant_dishes set reced = 'N' where did = {$did}";
    }
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('修改成功！');location.href='merchant_center.php';</script>";
    } else {
        echo "<script>alert('修改失败！');location.href='merchant_center.php';</script>";
    }
}
//删除菜品
function delete_dishes($did){
    require_once('db_fns.php');
    $conn = db_connect();
    $sql = "delete from merchant_dishes where did={$did}";
    $result = $conn->query($sql);
    if ($result) {
        echo "<script>alert('删除成功！');location.href='merchant_center.php';</script>";
    } else {
        echo "<script>alert('删除失败！');location.href='merchant_center.php';</script>";
    }
}
//商家查看所有订单
function display_order(){
    require_once('db_fns.php');
    $conn = db_connect();
    $mid = get_mid();
    $sql = "CALL sp_query_order_by_mid({$mid});";
    $result = $conn->query($sql);
    return $result;
}

//商家查看订单详细信息
function display_order_detail($oid){
    require_once('db_fns.php');
    $conn = db_connect();
    $mid = get_mid();
    $sql = "CALL sp_query_order_detail_by_mid_oid({$mid},{$oid});";
    $result = $conn->query($sql);
    return $result;
}
//获取mid
function get_mid(){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $uid = $_SESSION['uid'];
    $sql = "select mid from user where uid = {$uid}";
    $mid = $conn->query($sql)->fetch_array()['mid'];
    return $mid;
}
//获取菜品查询结果
function get_search_dishes($words){
    require_once('db_fns.php');
    $conn = db_connect();
    if(session_status() == PHP_SESSION_NONE) {
        session_start();
    }
    if (!isset($_SESSION['uid'])) {
        return false;
    }
    $uid = $_SESSION['uid'];
    $sql = "select mid from user where uid = {$uid}";
    $mid = $conn->query($sql)->fetch_array()['mid'];
    $sql = "select * from merchant_dishes where mid = {$mid} and name like '%{$words}%'";
    $result = $conn->query($sql);
    return $result;
}
?>
